using UnityEngine;
using System.Collections;

public class Buttons : MonoBehaviour {
	
	public static TreeNode root;
	public static TreeNode[] list;
    public int startX;
    public int startY;
    public int TdungeonWidth = 100;
    public int TdungeonHeight = 100;
    public static float maxRoomSize = 100;
    public static int minRoomSize = 5;
	private int splitNum = 0;
	int pIndex = 0;
	
	public Texture2D black;
	public Texture2D grey;
	public Texture2D green;
	public Texture2D red;

	
	 //the texture to create the tiles from
    public Texture2D baseMap;
    //default tile size.  the included texture has 16x16 tiles
    public int tileSize = 16;
    //the tiles
    TileMap tileMap;
    //the tile date telling which tiles to draw where
    int[] tileData;
	
	int[] tempTileData;
    //how large is the dungeon **IN TILES**
    //assumes a square dungeon
   // public int dungeonWidth = 10;

    //positioning of the map on screen
    public int screenOffsetX = 20;
    public int screenOffsetY = 20;

    //the offset used to determine which part of the map to show
    public int mapOffsetX = 0;
    public int mapOffsetY = 0;


	// Use this for initialization
	void Start () {
		tileMap = new TileMap(baseMap, tileSize);
		tileData = new int[TdungeonWidth * TdungeonWidth];
		tempTileData = new int[TdungeonWidth * TdungeonWidth];
        for (int i = 0; i < tileData.Length; i++)
        {
            //yield return null;
            tileData[i] = Random.Range(0, 7);
        }
		 
		
		splitNum = TdungeonWidth / 33;
		TreeNode.ratio = 2;
		root = new TreeNode(startX,startY,TdungeonWidth,TdungeonHeight);
		list = new TreeNode[3];
		list[0] = root;
		for(int i = 0; i < splitNum; i++)
		{
			root.split();
		}
		root.generateRooms();
		//print(TreeNode.counter);
		list[0].generateCorridors(TreeNode.counter);
		
		//tileData = root.tileMap;
		for(int i = 0; i < TdungeonWidth * TdungeonWidth; i++)
		{
			if(i < (TdungeonWidth * TdungeonWidth) / 2)
			{
				tileData[i] = 0;
			}
			else
			{
				tileData[i] = 1;
			}
			
			for(int j = 0; j < TreeNode.counter; j++)
			{
				if(i % TdungeonWidth >= list[j].roomX)
				{
					if(i % TdungeonWidth <= list[j].roomX + list[j].roomWidth)
					{
						if(i / TdungeonWidth >= list[j].roomY)
						{
							if(i / TdungeonWidth <= list[j].roomY + list[j].roomHeight)
							{
								if(j < TreeNode.counter/2)
								{
									tileData[i] = 3;
								}
								else
								{
									tileData[i] = 2;
								}
							}
						}
					}
				}
			}
		}
		for(int i = 0; i < TdungeonWidth * TdungeonWidth; i++)tempTileData[i] = tileData[i];
		
		placePlayer();
		mapOffsetX = (pIndex % TdungeonWidth) - 15;
		mapOffsetY = (pIndex / TdungeonWidth) - 15;
	}
	
	// Update is called once per frame
	void Update () {
	 	if (Input.GetKeyDown(KeyCode.LeftArrow))
		{
			if(tempTileData[pIndex -1] > 1)
			{
				mapOffsetX--;
				tileData[pIndex] = tempTileData[pIndex];
				pIndex--;
				tileData[pIndex] = 8;
			}
		}
        if (Input.GetKeyDown(KeyCode.RightArrow))
		{
			if(tempTileData[pIndex +1] > 1)
			{
				mapOffsetX++;
				tileData[pIndex] = tempTileData[pIndex];
				pIndex++;
				tileData[pIndex] = 8;
			}
		}
        if (Input.GetKeyDown(KeyCode.UpArrow))
		{
			if(tempTileData[pIndex - TdungeonWidth] > 1)
			{
				mapOffsetY--;
				tileData[pIndex] = tempTileData[pIndex];
				pIndex -= TdungeonWidth;
				tileData[pIndex] = 8;
			}
		}
			
        if (Input.GetKeyDown(KeyCode.DownArrow))
		{
			if(tempTileData[pIndex + TdungeonWidth] > 1)
			{
				mapOffsetY++;
				tileData[pIndex] = tempTileData[pIndex];
				pIndex += TdungeonWidth;
				tileData[pIndex] = 8;
			}
		}
		
		if (Input.GetKeyDown(KeyCode.N)) createNewDungeon();
	}
	
	 public void OnGUI()
    {
        if (Event.current.type.Equals(EventType.repaint))
        {
            DrawStuff(mapOffsetX, mapOffsetY, 30, 30);
        }

        if (!tileMap.done)
        {
            GUI.Label(new Rect(20, 20, 100, 30), "Creating Tile " + tileMap.counter.ToString());
        }
		
		/*if(Event.current.type.Equals(EventType.Repaint))
		{
			for(int i = 0; i < TreeNode.counter; i++)
			{
				if(list[i].horizontal)
				{
					//Draw a horizontal line starting from the random X and Y coordinate inside of the 
					//parent to the width or height of the room.The split line has a thickness of 5 pixels
					//for readability.
					Graphics.DrawTexture(new Rect(list[i].rightChild.x,
					                              list[i].rightChild.y,
					                              list[i].width,2),black);
				}
				else if(list[i].rightChild != null)
				{
					Graphics.DrawTexture(new Rect(list[i].rightChild.x,
					                              list[i].rightChild.y, 2,
					                              list[i].height),black);
				}
				
			}
			
			
			
		}
		if(Event.current.type.Equals(EventType.Repaint))
		{
			
			for(int i = TreeNode.counter/2; i < TreeNode.counter; i++)//only colors rooms
			{
				Graphics.DrawTexture(new Rect(list[i].roomX, list[i].roomY, list[i].roomWidth, list[i].roomHeight),green);
				//print(i + " " + list[i].roomX + ", " + list[i].roomY);
			}
			
			for(int j = 0; j < TreeNode.counter/2; j++)//only colors corridors
			{
				Graphics.DrawTexture(new Rect(list[j].roomX, list[j].roomY, list[j].roomWidth, list[j].roomHeight),red);
				//print(i + " " + list[i].roomX + ", " + list[i].roomY);
			}
		}
		
	*/

    }

    public void DrawStuff(int xOffset, int yOffset, int width, int height)
    {
        //check to ensure you're not off map
        if (xOffset + width > TdungeonWidth)
        {
            xOffset = TdungeonWidth - width;
        }
        if (yOffset + height > TdungeonWidth)
        {
            yOffset = TdungeonWidth - height;
        }
        xOffset = Mathf.Max(0, xOffset);
        yOffset = Mathf.Max(0, yOffset);

        Rect screenTarget = new Rect();
        //width and height is always the tile size
        screenTarget.width = tileMap.tileSize;
        screenTarget.height = tileMap.tileSize;
        int targetIndex = 0;


        for (int row = yOffset; row < yOffset + height; row++)
        {
            for (int col = xOffset; col < xOffset + width; col++)
            {
                screenTarget.x = (col - xOffset) * tileMap.tileSize + screenOffsetX;
                screenTarget.y = (row - yOffset) * tileMap.tileSize + screenOffsetY;
                //targetIndex gives us the position in tileData we need to look at
                //to find which tile to draw.
                targetIndex = col + (row * TdungeonWidth);
			
                Graphics.DrawTexture(screenTarget, tileMap.tiles[tileData[targetIndex]]);
				//Graphics.DrawTexture(screenTarget, tileMap.tiles[0]);
				
				
            }
        }
    }
	
	public void createNewDungeon()
	{
		TreeNode.counter = 1;
		tileMap = new TileMap(baseMap, tileSize);
		tileData = new int[TdungeonWidth * TdungeonWidth];
        for (int i = 0; i < tileData.Length; i++)
        {
            //yield return null;
            tileData[i] = Random.Range(0, 7);
        }
		 
		
		splitNum = TdungeonWidth / 33;
		TreeNode.ratio = 2;
		root = new TreeNode(startX,startY,TdungeonWidth,TdungeonHeight);
		list = new TreeNode[3];
		list[0] = root;
		for(int i = 0; i < splitNum; i++)
		{
			root.split();
		}
		root.generateRooms();
		//print(TreeNode.counter);
		list[0].generateCorridors(TreeNode.counter);
		
		//tileData = root.tileMap;
		for(int i = 0; i < TdungeonWidth * TdungeonWidth; i++)
		{
			tileData[i] = 0;
			
			for(int j = 0; j < TreeNode.counter; j++)
			{
				if(i % TdungeonWidth >= list[j].roomX)
				{
					if(i % TdungeonWidth <= list[j].roomX + list[j].roomWidth)
					{
						if(i / TdungeonWidth >= list[j].roomY)
						{
							if(i / TdungeonWidth <= list[j].roomY + list[j].roomHeight)
							{
								if(j < TreeNode.counter/2)
								{
									tileData[i] = 3;
								}
								else
								{
									tileData[i] = 2;
								}
							}
						}
					}
				}
			}
		}
		for(int i = 0; i < TdungeonWidth * TdungeonWidth; i++)tempTileData[i] = tileData[i];
		placePlayer();
		mapOffsetX = (pIndex % TdungeonWidth) - 15;
		mapOffsetY = (pIndex / TdungeonWidth) - 15;
	}
	
	public void placePlayer()
	{
		bool isGood = false;
		while (isGood == false)
		{
			pIndex = Random.Range(0,TdungeonWidth*TdungeonWidth);
			if(tileData[pIndex] > 1) isGood = true;
		}
		tileData[pIndex] = 8;
	}
}

	


